package org.jeecg.modules.monitoring.j_monitoring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.LicenseSoftware;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareModuleSetting;
import org.jeecg.modules.monitoring.j_monitoring.entity.software.SoftwareSetting;
import org.jeecg.modules.monitoring.j_monitoring.facilities.FacilitiesToken;
import org.jeecg.modules.monitoring.j_monitoring.service.ISoftwareSettingService;
import org.jeecg.modules.monitoring.j_monitoring.service.impl.LicenseSoftwareServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "软件许可大屏")
@RestController
@RequestMapping("/software")
@Slf4j
public class SoftwareController {

    @Resource
    private LicenseSoftwareServiceImpl licenseSoftwareService;
    @Resource
    private FacilitiesToken facilitiesToken;

    @Autowired
    private ISoftwareSettingService softwareSettingService;

    @ApiOperation(value = "大屏专业软件列表", notes = "大屏专业软件列表")
    @GetMapping(value = "/querySoftwareList")
    public Result querySoftwareList(SoftwareSetting licenseSoftware,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpServletRequest req) {
        QueryWrapper<SoftwareSetting> queryWrapper = QueryGenerator.initQueryWrapper(licenseSoftware, req.getParameterMap());
        queryWrapper.orderByAsc("sort_no");
        Page<SoftwareSetting> page = new Page<SoftwareSetting>(pageNo, pageSize);
        IPage<SoftwareSetting> pageList = softwareSettingService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "大屏专业软件列表", notes = "大屏专业软件列表")
    @GetMapping(value = "/querySoftAll")
    public Result querySoftAll(SoftwareSetting licenseSoftware,HttpServletRequest req) {
        QueryWrapper<SoftwareSetting> queryWrapper = QueryGenerator.initQueryWrapper(licenseSoftware, req.getParameterMap());
        queryWrapper.orderByAsc("sort_no");
        List<SoftwareSetting> dataList = softwareSettingService.list(queryWrapper);
        return Result.OK(dataList);
    }


    @ApiOperation(value = "大屏专业软件模块使用", notes = "大屏专业软件模块使用")
    @GetMapping(value = "/modelUsed")
    public Result modelUsed(String softwareName) {

        return Result.OK(licenseSoftwareService.modelUsed(softwareName));
    }

    @ApiOperation(value = "大屏专业软件使用频次", notes = "大屏专业软件使用频次")
    @GetMapping(value = "/usedFrequency")
    public Result usedFrequency(String softwareName, String place) {
        return Result.OK(licenseSoftwareService.usedFrequency(softwareName, place));
    }


    @ApiOperation(value = "大屏专业软件用户使用表", notes = "大屏专业软件用户使用表")
    @GetMapping(value = "/softUser")
    public Result softUser(String softwareName) {
        return Result.OK(licenseSoftwareService.softUser(softwareName));
    }


    @ApiOperation(value = "大屏专业软件情况对比", notes = "大屏专业软件情况对比")
    @GetMapping(value = "/softContrast")
    public Result softContrast(String softwareName) {
        return Result.OK(licenseSoftwareService.softContrast(softwareName));
    }


    @ApiOperation(value = "大屏专业软件下筛选模块", notes = "大屏专业软件下筛选模块")
    @GetMapping(value = "/softModule")
    public Result softModule(String softwareName) {
        return Result.OK(licenseSoftwareService.softModule(softwareName));
    }


    @ApiOperation(value = "大屏专业软件使用情况对比", notes = "大屏专业软件使用情况对比")
    @GetMapping(value = "/softUsage")
    public Result softUsage(String softwareName,
                            String moduleName,
                            String moduleId,
                            String startDate,
                            String endDate) {

        if (StringUtil.isEmpty(startDate) && StringUtil.isEmpty(endDate)){
            startDate = DateUtils.getLastYearToday();
        }
        return Result.OK(licenseSoftwareService.softUsage(softwareName,moduleName,moduleId,startDate,endDate));
    }

    @ApiOperation(value = "大屏地图红字统计", notes = "大屏地图红字统计")
    @GetMapping(value = "/softNumber")
    public Result softNumber() {

        return Result.OK(licenseSoftwareService.softNumber());
    }

    @GetMapping(value = "/test")
    public Result test() {
        facilitiesToken.saveTemLog();
        return Result.OK();
    }


}
